<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");

require_once '../../../lib/php/vars.php';
require_once 'class.upload.php';

if (isset($_POST['file'])
	&& isset($_POST['folder'])
	&& isset($_POST['crop_x'])
	&& isset($_POST['crop_y'])
	&& isset($_POST['crop_w'])
	&& isset($_POST['crop_h'])) {
	$file = $documentRoot.$sitePath.$_POST['file'];
	$targetPath = $documentRoot.$sitePath.$imageRoot.$_POST['folder'];

	$handle = new upload($file);
	if ($handle->uploaded) {
		
		// save uploaded image with a new name
		$handle->file_name_body_pre 	= 'thumb_';
		$handle->file_overwrite			= true;
		$handle->file_auto_rename		= false;
		$handle->image_resize			= true;
		$handle->image_x				= 75;
		$handle->image_y				= 75;
		
		$handle->jcrop					= true;
		$handle->rect_w					= $_POST['crop_w']; 
		$handle->rect_h					= $_POST['crop_h']; 
		$handle->posX					= $_POST['crop_x']; 
		$handle->posY					= $_POST['crop_y'];
		$handle->jpeg_quality			= 60;
		$handle->Process($targetPath);
		if ($handle->processed) {
			$json = array('result' 			=> 1,
							'dir'			=> $handle->file_dst_path,
							'filename'		=> $handle->file_dst_name,
							'pathname'		=> $imageRoot.$_POST['folder'].$handle->file_dst_name,
							'imagewidth' 	=> $handle->image_dst_x,
							'imageheight'	=> $handle->image_dst_y,
							'response'		=> 'image "'.$file.'" resized and renamed to "'.$handle->file_dst_pathname.'".'
							);
		} else {
			$json = array('result' 		=> 0,
							'response'	=> 'image processing failed, error : ' . $handle->error
			);
		}
		$handle->clean();
	} else {
		$json = array('result' => 0,
						'response'	=> 'upload failed, error : ' . $handle->error
		);
	}
} else {
	$json = array('result' => 0,
					'response'	=> 'missing parameter, error : ' . $handle->error
	);
}
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
?>